{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unit testing ?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LetsUpgrade.in/hack"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Unit testing means, testing a peice of code before it is going live in \n",
    "any place, and testing it with different use cases, \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PyLint "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pylint in /opt/anaconda3/lib/python3.7/site-packages (2.4.2)\n",
      "Requirement already satisfied: isort<5,>=4.2.5 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (4.3.21)\n",
      "Requirement already satisfied: mccabe<0.7,>=0.6 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (0.6.1)\n",
      "Requirement already satisfied: astroid<2.4,>=2.3.0 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (2.3.1)\n",
      "Requirement already satisfied: lazy-object-proxy==1.4.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.2)\n",
      "Requirement already satisfied: typed-ast<1.5,>=1.4.0; implementation_name == \"cpython\" and python_version < \"3.8\" in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.1)\n",
      "Requirement already satisfied: wrapt==1.11.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.11.2)\n",
      "Requirement already satisfied: six==1.12 in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.12.0)\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.2.3 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "! pip install pylint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting some_code.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile some_code.py\n",
    "'''\n",
    "It is a peice of non-sense example code\n",
    "'''\n",
    "A = 1\n",
    "B = 2\n",
    "\n",
    "def addition(num1, num2):\n",
    "    '''\n",
    "    Hey this is a Simple Addition Fuction\n",
    "    '''\n",
    "    print(num1 + num2)\n",
    "\n",
    "addition(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "--------------------------------------------------------------------\r\n",
      "Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! pylint \"some_code.py\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Steps \n",
    "\n",
    "1. YOU GUYS Develope a Program \n",
    "2. Now you want to test each function of the program \n",
    "3. How to test it ?\n",
    "4. We will use UNITTESTING to do that \n",
    "5. How to make unitesting ?\n",
    "6. Make another python file \n",
    "7. then run that Python file, so you will get it "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting capitalizeText.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile capitalizeText.py\n",
    "\n",
    "def capText(string_To_Cap):\n",
    "    return string_To_Cap.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Sai'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import capitalizeText\n",
    "\n",
    "capitalizeText.capText(\"sai\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting testCapText.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile testCapText.py\n",
    "\n",
    "import unittest\n",
    "import capitalizeText\n",
    "\n",
    "class testCapTitle(unittest.TestCase):\n",
    "    def testSingleWord(self):\n",
    "        abc = \"sai\"\n",
    "        result = capitalizeText.capText(abc)\n",
    "        self.assertEquals(result, \"Sai\")\n",
    "        \n",
    "    def testingMultipleWords(self):\n",
    "        xyz = \"manisha bhagat\"\n",
    "        result = capitalizeText.capText(xyz)\n",
    "        self.assertEquals(result,\"Manisha Bhagat\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "testCapText.py:9: DeprecationWarning: Please use assertEqual instead.\r\n",
      "  self.assertEquals(result, \"Sai\")\r\n",
      ".F\r\n",
      "======================================================================\r\n",
      "FAIL: testingMultipleWords (__main__.testCapTitle)\r\n",
      "----------------------------------------------------------------------\r\n",
      "Traceback (most recent call last):\r\n",
      "  File \"testCapText.py\", line 14, in testingMultipleWords\r\n",
      "    self.assertEquals(result,\"Manisha bhagat\")\r\n",
      "AssertionError: 'Manisha Bhagat' != 'Manisha bhagat'\r\n",
      "- Manisha Bhagat\r\n",
      "?         ^\r\n",
      "+ Manisha bhagat\r\n",
      "?         ^\r\n",
      "\r\n",
      "\r\n",
      "----------------------------------------------------------------------\r\n",
      "Ran 2 tests in 0.001s\r\n",
      "\r\n",
      "FAILED (failures=1)\r\n"
     ]
    }
   ],
   "source": [
    "! python testCapText.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generators"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getCloseByEvenNumber(lst):\n",
    "    for item in lst:\n",
    "        if item%2==0:\n",
    "            return item\n",
    "        else:\n",
    "            return item+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getCloseByEvenNumber([5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getCloseByEvenNumber([4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = list(range(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n"
     ]
    }
   ],
   "source": [
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# I have a List of 100 Numebrs, I want to pass all of them \n",
    "# into the getNearByEvenNumber() Function \n",
    "# THen I want to store those in a new List "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getCloseByEvenNumber(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getCloseByEvenNumber(lst):\n",
    "    for item in lst:\n",
    "        if item%2==0:\n",
    "            return item\n",
    "        else:\n",
    "            return item+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getCloseByEvenNumber(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getCloseByEvenNumberGen(lst):\n",
    "    for item in lst:\n",
    "        if item%2==0:\n",
    "            yield item\n",
    "        else:\n",
    "            yield item+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 16, 18, 18, 20, 20, 22, 22, 24, 24, 26, 26, 28, 28, 30, 30, 32, 32, 34, 34, 36, 36, 38, 38, 40, 40, 42, 42, 44, 44, 46, 46, 48, 48, 50, 50, 52, 52, 54, 54, 56, 56, 58, 58, 60, 60, 62, 62, 64, 64, 66, 66, 68, 68, 70, 70, 72, 72, 74, 74, 76, 76, 78, 78, 80, 80, 82, 82, 84, 84, 86, 86, 88, 88, 90, 90, 92, 92, 94, 94, 96, 96, 98, 98, 100]\n"
     ]
    }
   ],
   "source": [
    "print(list(getCloseByEvenNumberGen(lst)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
